package com.gz.mylife.app;

import java.lang.ref.WeakReference;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.zl.smartmall.library.IGZAddress;
import com.zl.smartmall.library.listener.ApplyHouseListenser;
import com.zl.smartmall.library.po.BuildingsInfo;
import com.zl.smartmall.library.po.DictCityInfo;
import com.zl.smartmall.library.po.DictDistrictInfo;
import com.zl.smartmall.library.po.DictProvinceInfo;
import com.zl.smartmall.library.po.GroupsInfo;
import com.zl.smartmall.library.po.HousesInfo;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SelectCommunityAddressActivity extends BaseActivity implements OnClickListener{
	private TextView tv_province;
	private TextView tv_city;
	private TextView tv_distrist;
	private TextView mTvFloor;
	private TextView tv_zutuan;
	private TextView tv_house;
	private Button mBtnNext;
	//private GZCommunity commnuityAddress;
	private DictProvinceInfo provinceInfo;
	private DictCityInfo cityInfo;
	private DictDistrictInfo districtInfo;
	private GroupsInfo groupsInfo;
	private BuildingsInfo buildingsInfo;
	private HousesInfo housesInfo;
	private Dialog mLoadingData;
	
	private int houseCmid = -1;
	private int houseId = -1;
	private boolean unbind;
	private EditText myphone;
	private MyApplyHouseListener mListener;
	public static int province = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selectcommunityaddress);
		String titlename = getIntent().getStringExtra("titlename");
		unbind = getIntent().getBooleanExtra("unbind", false);
		setActionbarTitle(titlename, false);
		mListener = new MyApplyHouseListener(this);
		initView();
	}

	private void initView() {
		mLoadingData = new Dialog(this, R.style.DialogTheme);
		mLoadingData.setContentView(R.layout.loading_data_dialog2);
		
		//findViewById(R.id.rl_select_community).setOnClickListener(this);//小区
		findViewById(R.id.rl_select_province).setOnClickListener(this);//省级
		findViewById(R.id.rl_select_city).setOnClickListener(this);//市级
		findViewById(R.id.rl_select_district).setOnClickListener(this);//区县
		findViewById(R.id.rl_select_floor).setOnClickListener(this);//楼
		findViewById(R.id.rl_select_zutuan).setOnClickListener(this);//组团
		findViewById(R.id.rl_select_house).setOnClickListener(this);//房间
		mBtnNext = (Button) findViewById(R.id.btn_next);
		mBtnNext.setOnClickListener(this);
		
		tv_province  = (TextView) findViewById(R.id.tv_province);
		tv_city = (TextView) findViewById(R.id.tv_city);
		tv_distrist = (TextView) findViewById(R.id.tv_district);
		//mTvCommunity = (TextView) findViewById(R.id.tv_community);
		tv_zutuan = (TextView)findViewById(R.id.tv_zutuan);
		mTvFloor = (TextView) findViewById(R.id.tv_floor);
		tv_house = (TextView) findViewById(R.id.tv_house);
		myphone = (EditText) findViewById(R.id.myphone);
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mListener.release();
	}
	
	@Override
	public void onClick(View v) {
		Intent intent ;
		switch (v.getId()) {
		case R.id.btn_next://点击下一步请求验证码
			if("".equals(tv_house.getText().toString())){
				Toast.makeText(getApplicationContext(), "请选择房间信息", Toast.LENGTH_LONG).show();
			} else if ("".equals(myphone.getText().toString().trim())) {
				Toast.makeText(getApplicationContext(), "请输入手机号码", Toast.LENGTH_LONG).show();
			} else {
				mLoadingData.show();
				IGZAddress.getInstance().applyHouse(houseCmid, houseId,myphone.getText().toString().trim(), mListener);
			}
			
			break;
		case R.id.rl_select_province://选择省级
			/*intent = new Intent();
			intent.setClass(getApplicationContext(), SelectCommunityActivity.class);
			startActivityForResult(intent,1);*/
			break;
		case R.id.rl_select_city://选择市级
			/*if(provinceInfo == null){
				Toast.makeText(this, "请先选择省份", Toast.LENGTH_SHORT).show();
				break;
			}
			intent = new Intent();
			intent.setClass(getApplicationContext(), SelectCityActivity.class);
			intent.putExtra("province_id", provinceInfo.getProvinceid());
			startActivityForResult(intent,110);*/
			break;
		case R.id.rl_select_district://选择区县
			/*if(cityInfo == null){
				Toast.makeText(this, "请先选择城市", Toast.LENGTH_SHORT).show();
				break;
			}
			intent = new Intent();
			intent.setClass(getApplicationContext(), SelectDistrictActivity.class);
			intent.putExtra("city_id", cityInfo.getCityid());
			startActivityForResult(intent,111);*/
			break;
		case R.id.rl_select_zutuan://小区名称
			/*if(districtInfo == null){
				Toast.makeText(this, "请先选择区县", Toast.LENGTH_SHORT).show();
				break;
			}*/
			intent = new Intent();
			intent.setClass(getApplicationContext(), SelectGroupsActivity.class);
			//intent.putExtra("district_id", districtInfo.getDistrictid());
			startActivityForResult(intent,107);
			break;
		case R.id.rl_select_floor://楼号
			if(groupsInfo == null){
				Toast.makeText(this, "请先选择小区", Toast.LENGTH_SHORT).show();
				break;
			}
			intent = new Intent();
			intent.setClass(getApplicationContext(), SelectBuildingsActivity.class);
			intent.putExtra("groupid", groupsInfo.getId());
			startActivityForResult(intent,108);
			break;
		case R.id.rl_select_house://房间号
			if(buildingsInfo == null){
				Toast.makeText(this, "请先选择楼牌号", Toast.LENGTH_SHORT).show();
				break;
			}
			intent = new Intent();
			intent.setClass(getApplicationContext(), SelectHouseActivity.class);
			intent.putExtra("buildingid", buildingsInfo.getId());
			startActivityForResult(intent,106);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int resultcode, Intent intent) {
		switch (resultcode) {
		case 109:
			provinceInfo = intent.getParcelableExtra("obj");
			tv_province.setText(provinceInfo.getProvincename());
			houseCmid = -1;
			houseId = -1;
			tv_city.setText("");
			tv_distrist.setText("");
			tv_zutuan.setText("");
			mTvFloor.setText("");
			tv_house.setText("");
			break;
		case 110:
			cityInfo = intent.getParcelableExtra("obj");
			tv_city.setText(cityInfo.getCityname());
			houseCmid = -1;
			houseId = -1;
			tv_distrist.setText("");
			tv_zutuan.setText("");
			mTvFloor.setText("");
			tv_house.setText("");
			break;
		case 111:
			districtInfo = intent.getParcelableExtra("obj");
			tv_distrist.setText(districtInfo.getDistrictname());
			houseCmid = -1;
			houseId = -1;
			tv_zutuan.setText("");
			mTvFloor.setText("");
			tv_house.setText("");
			break;
		case 101:
			String houseName = intent.getStringExtra("name");
			houseCmid = intent.getIntExtra("cmid", 0);
			houseId = intent.getIntExtra("id", 0);
			mTvFloor.setText(houseName);
			mBtnNext.setClickable(true);
			break;
		case 201:
			if(unbind){
				IGZAddress.getInstance().setIDAndName(houseCmid, provinceInfo.getProvincename());
			}
			setResult(201);
			finish();
			break;
		case 107:
			groupsInfo = intent.getParcelableExtra("obj");
			String groupname = intent.getStringExtra("groupname");
			tv_zutuan.setText(groupname);
			houseCmid = -1;
			houseId = -1;
			mTvFloor.setText("");
			tv_house.setText("");
			break;	
		case 108:
			buildingsInfo = (BuildingsInfo) intent.getSerializableExtra("obj1");
			mTvFloor.setText(buildingsInfo.getName());
			houseCmid = -1;
			houseId = -1;
			tv_house.setText("");
			break;
		case 106:
			housesInfo = (HousesInfo) intent.getSerializableExtra("obj");
			houseCmid = housesInfo.getCmid();
			houseId = housesInfo.getId();
			tv_house.setText(housesInfo.getHouseaddress());
			break;	

		default:
			break;
		}
	}
	
	private static class MyApplyHouseListener extends ApplyHouseListenser{
		private SelectCommunityAddressActivity mOuter;
		
		public MyApplyHouseListener(SelectCommunityAddressActivity outer){
			this.mOuter = outer;
		}
		
		public void release(){
			new WeakReference<SelectCommunityAddressActivity>(mOuter);
		}

		@Override
		public void onResponseSuccess() {
			if(mOuter == null){
				return;
			}
			mOuter.mLoadingData.dismiss();
			Intent intent = new Intent(mOuter.getApplicationContext(), AddHouseActivity.class);//进入提交手机验证码的页面
			intent.putExtra("cmid",mOuter.houseCmid);
			intent.putExtra("id",mOuter.houseId);
			intent.putExtra("phone", mOuter.myphone.getText().toString().trim());//接收验证码的手机号码
			mOuter.startActivityForResult(intent, 1);
		}

		@Override
		public void onNoNetWork() {
			if(mOuter == null){
				return;
			}
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter.getApplicationContext(), Define.MSG_NO_NETWORK, 0).show();
		}

		@Override
		public void onResponseFailed() {
			if(mOuter == null){
				return;
			}
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter.getApplicationContext(), "请求失败", 0).show();
		}

		@Override
		public void passthree() {
			// TODO Auto-generated method stub 
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter.getApplicationContext(), "同一手机号当天发送验证码不能超过5次", 0).show();
		}

		@Override
		public void passtwo() {
			// TODO Auto-generated method stub
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter.getApplicationContext(), "同一ip手机当天发送验证码不能超过10次", 0).show();
		}
		
	}
	
}
